﻿using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Rendering.Universal;

namespace Miao
{
    public class AudioLoadReader
    {

        private WaveStream readerStream;
        /// <summary>
        /// 将int转为float
        /// </summary>
        private readonly SampleChannel sampleChannel;

        private readonly int destBytesPerSample;
        private readonly int sourceBytesPerSample;

        //private readonly int destBytesPerSample;
        //private readonly int sourceBytesPerSample;

        private readonly long length;

        public AudioClip clip;


        public long Postion
        {
            get
            {
                return readerStream.Position;
            }
            set
            {
                readerStream.Position = value;
            }
        }


        public AudioLoadReader(Stream stream, AudioType audioType)
        {
            CreateReaderStream(stream, audioType);
            sampleChannel = new SampleChannel(readerStream, forceStereo: false);


        }
        private void CreateReaderStream(Stream stream, AudioType audioType)
        {

            switch (audioType)
            {
                case AudioType.WAV:
                    readerStream = new WaveFileReader(stream);

                    break;
                case AudioType.MPEG:
                    readerStream = new Mp3FileReader(stream);

                    break;
                case AudioType.AIFF:
                    readerStream = new AiffFileReader(stream);
                    break;
            }
        }

        public void Read(float[] data)
        {

            sampleChannel.Read(data, 0, data.Length);


        }

        public int Read(float[] buffer, int offset, int count)
        {
            return sampleChannel.Read(buffer, offset, count);
        }


        //static public AudioClip Load(FileStream stream,string name, AudioType audioType)
        //{
            

        //    AudioLoadReader audioLoad = new AudioLoadReader(stream, audioType);



        //    WaveFormat waveFormat = audioLoad.readerStream.ToSampleProvider().WaveFormat;
        //    int sampleRate = waveFormat.SampleRate;
        //    int channels = waveFormat.Channels;
        //    //int lengthSamples = (int)(audioLoad.readerStream.Length / (waveFormat.BitsPerSample / 8));
        //   // int lengthSamples = 
        //  //audioLoad.clip = AudioClip.Create(stream.Name, samplesCount, channels, waveFormat.SampleRate, true,
        //  //    (float[] target) =>
        //  //    {
        //  //        audioLoad.Read(target, 0, target.Length);
        //  //    });
        //  //    , (int target) =>
        //  //    {
        //  //        audioLoad.Seek(target, SeekOrigin.Begin);
        //  //    });
        //  //长度= audioSource.clip.samples * audioSource.clip.channels;
        //  //float[] samples = new float[sampleRate * channels];

        //  //  audioLoad.clip = AudioClip.Create(name, (int)audioLoad.readerStream.Length, channels, waveFormat.SampleRate, false);
        //  //  //audioLoad.clip = AudioClip.Create(name, lengthSamples, channels, waveFormat.SampleRate, false);

        //  //  audioLoad.Read(samples);

        //  //  audioLoad.clip.SetData(samples,0);



        //  //  return audioLoad.clip;
        //}



    }
}
